package com.three.crm.fragment;

import android.content.Context;
import android.os.Bundle;


import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.three.crm.R;
import com.three.crm.base.BaseFragment;
import com.three.crm.bean.UserBean;
import com.three.crm.constant.AppConstant;
import com.three.crm.http.CommonResultCallBack;
import com.three.crm.page.setting.AboutUsActivity;
import com.three.crm.require.AppRequireService;
import com.three.crm.util.AppManager;
import com.three.crm.util.GsonUtils;
import com.three.crm.util.SignUtil;
import com.three.crm.util.XpopupUtil;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

/**
 * 我的
 * create by lqd on 2024-01-12.
 */
public class MeFragment extends BaseFragment {

    public MeFragment() {}

    public static MeFragment newInstance(int index, String title) {
        MeFragment fragment = new MeFragment();
        Bundle args = new Bundle();
        args.putInt(AppConstant.INDEX, index);
        args.putString(AppConstant.KEY_TITLE, title);
        fragment.setArguments(args);
        return fragment;
    }

    private final static int REQUEST_QUERY = 101;
    private TextView tvTitle,tvName,tvPhone;
    private ImageView ivAvatar;

    @Override
    protected void initArguments(Bundle bundle) {}

    @Override
    protected int bindLayout() {
        return R.layout.fragment_me;
    }

    @Override
    protected void initViews(View view, Bundle savedInstanceState) {
        tvTitle = $(R.id.tv_title);
        tvTitle.setText(R.string.me);
        $(R.id.iv_left).setVisibility(View.GONE);

        tvName = $(R.id.tv_name);
        tvPhone = $(R.id.tv_phone);
        ivAvatar = $(R.id.iv_avatar);
    }

    @Override
    public void initListener() {
        $(R.id.ll_about).setOnClickListener(this);
        $(R.id.btn_exit).setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()){
            case R.id.ll_about:
                startActivity(AboutUsActivity.class);
                break;
            case R.id.btn_exit:
                onExit();
                break;
        }
    }

    private void onExit() {
        XpopupUtil.showOnce(getActivity(), getString(R.string.Are_you_sure_you_want_to_quit), new XpopupUtil.OnPopupOkListener() {
            @Override
            public void onPopupConfirm() {
                exitTing();
            }
        });
    }

    private void exitTing() {
        showLoading(getString(R.string.exitting));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                hideLoading();
                AppManager.reLogin(getActivity());
            }
        },500);
    }

    @Override
    protected void initData(Context context) {
        onQueryUserData();
    }

    private void onQueryUserData() {
        showLoading(getString(R.string.loading));
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userToken",AppManager.getToken());
            jsonObject.put("timestamp",getTimestamp());
            String sign = SignUtil.genSign(jsonObject);
            jsonObject.put("sign",sign);
            AppRequireService.queryUserInfo(getActivity(), REQUEST_QUERY, jsonObject, new CommonResultCallBack(getActivity()) {
                @Override
                public void onResult(String result, int requireCode) {
                    hideLoading();
                    if (requireCode == REQUEST_QUERY) {
                        UserBean bean = GsonUtils.parseJsonWithGson(result, UserBean.class);
                        setUserInfo(bean);
                    }
                }

                @Override
                public void onConnectTimeOut(Call call, Exception e, int requireCode) {
                    hideLoading();
                    showToast(e.getMessage());
                }

                @Override
                public void onFail(String msg, int requireCode) {
                    hideLoading();
                    showToast(msg);
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void setUserInfo(UserBean bean) {
        if (bean != null) {
            tvName.setText("" + bean.getNickname());
            tvPhone.setText("" + bean.getPhone());
        }
    }
}